import time
import pygame as pg
from ombot import world
# import world
# from util import button

pg.init()
pg.font.init()
font = pg.font.SysFont('Times New Roman', 10)
color = pg.Color('dodgerblue2')


class Game:
    def __init__(self, win, mode=1):
        # self.display = display
        self.win = win

        # Game mode
        # self.HUMAN = 0
        # self.TRAIN = 1
        # self.AI = 2
        self.mode = mode

        # num of iteration
        self.epoch0 = 200  # human
        self.epoch1 = 500  # train
        self.epoch2 = 200  # AI

        # Creat a game world
        # self.world = world.World(display)
        self.world = world.World(win)  # !!!!!!!!还要改

    def show(self, epoch):
        self.win.fill((255, 255, 255))
        # self.display.fill(0)

        self.world.show()

        # show game mode
        if self.mode == 0:
            text = font.render("H", True, color)
            self.win.blit(text, (20, 0))
        # self.display.text("H",20,0,1)

        elif self.mode == 2:
            text = font.render("A", True, color)
            self.win.blit(text, (20, 0))
        # self.display.text("A",20,0,1)

        else:
            text = font.render("T", True, color)
            self.win.blit(text, (20, 0))
        # self.display.text("T",20,0,1)

        # show current epoch
        text1 = font.render(str(epoch), True, color)
        self.win.blit(text1, (40, 0))
        # self.display.text(str(epoch),40,0,1)

        pg.display.update()

    # self.display.show()

    def show_mode(self):
        self.win.fill((255, 255, 255))
        # self.display.fill(0)

        # show game mode
        if self.mode == 0:
            text = font.render("Human Control", True, color)
            self.win.blit(text, (15, 30))
        # self.display.text("Human Control",15,30,1)
        elif self.mode == 1:
            text = font.render("Robot Learning", True, color)
            self.win.blit(text, (10, 30))
        # self.display.text("Robot Learning",10,30,1)
        else:
            text = font.render("AI Control", True, color)
            self.win.blit(text, (30, 30))
        # self.display.text("AI Control",30,30,1)

        pg.display.update()
        # self.display.show()
        # sleep
        time.sleep(1)

    def run(self, mode=1):
        self.mode = mode
        if self.mode == 0:  # Human control
            self.run0()
        elif self.mode == 1:  # Robot Learning
            self.run1()
            # finish train, switch to mode 2
            self.run(2)
        elif self.mode == 2:  # AI control
            self.run2()

    # Human control
    def run0(self):
        self.show_mode()
        for i in range(self.epoch0):
            # world update
            self.world.update()

            # read human input, control the robot
            # if button.value() == 1:
            for event in pg.event.get():
                if event.type == pg.MOUSEBUTTONDOWN:
                    self.world.robot.jump()
            #
            # if True:
            #     self.world.robot.jump()

            # refresh screen
            self.show(i)

            # sleep
            time.sleep(0.01)

    # Robot Learning
    def run1(self):
        self.show_mode()
        for i in range(self.epoch1):
            # world update
            self.world.update()

            # learning
            # if self.world.robot.learn(button.value()) == True:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    pg.quit()
                else:
                    a = event.type == pg.MOUSEBUTTONDOWN
                    if self.world.robot.learn(a):
                        self.world.robot.n_collisions = 0
                        break  # end of learning

            # refresh screen
            self.show(i)

            # sleep
            time.sleep(0.08)
            # time.sleep_ms(10)

    # AI control
    def run2(self):
        self.show_mode()
        for i in range(self.epoch2):
            # quit game
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    pg.quit()
            # world update
            self.world.update()

            # AI decision
            self.world.robot.think()

            # refresh screen
            self.show(i)

            # sleep
            time.sleep(0.01)
